package com.wdc.redisUtils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class RedisUtils {
    private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);
    ConcurrentHashMap<String, String> redisService = new ConcurrentHashMap<>();

    public String get(String key) {
        log.info("redis get key:{} value: {}", key, redisService.get(key));
        return redisService.get(key);
    }

    public Boolean set(String key, String value) {
        if (key.isEmpty()) {
            log.info("redis set 失败 key key不能为空");
            throw new RuntimeException("key 不能为空");
        }

        log.info("redis set key: {} value: {} ", key, value);
        redisService.put(key, value);
        return true;

    }

    public void set(String key, String value, long timeout, TimeUnit timeUnit) {
        log.info("redis setNx key: {} value: {} timeout :{} timeUnit: {}", key, value, timeout, timeUnit);
        redisService.put(key, value);
    }

    public String del(String key) {
        log.info("redis delete key:{}", key);

        try {
            if (!redisService.containsKey(key)) {
                log.info("待删除的 key 不存在");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return redisService.remove(key);

    }



}
